/**
 * Created by Administrator on 2020/5/9.
 */
var mysql = require("mysql");
var request = require("request");
var table = "accident";
var name = "境内滑雪运动保险";
var url = "http://www.epicc.com.cn/eproduct/conproposal/calculate.do";

db = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "xuyanzhao",
    database: "product"
});
//打开数据库
db.connect();
//插入数据
function insertData(data) {
    STRING = "";
    VALUES = "";
    for (var key in data) {
        STRING += "`" + key + "`" + ",";
        VALUES += '"' + data[key] + '"' + ",";
    }
    fields = ( STRING.substring(0, STRING.length - 1));
    values = ( VALUES.substring(0, VALUES.length - 1));
    Sql = 'INSERT INTO `' + table + '`(' + fields + ')VALUES(' + values + ')';
    db.query(Sql, function (err, result) {
        if (err) {
            console.log("操作失败");
            return;
        }
        console.log(Sql);

    })

}
function genProductInfo() {
    var days = ["1d", "7d", "45d"];
    for (i = 0; i < days.length; i++) {
        data = {
            days: days[i],
            people_count: "1",
            accident_death: "100000",
            exigency_back: "20000",
            accident_medical: "20000",
            public_duty: "10000",
            luggage: "1200"
        };
        //执行爬虫
        httpRequest(data)

    }
    for (i = 0; i < days.length; i++) {
        data = {
            days: days[i],
            people_count: "1",
            accident_death: "200000",
            exigency_back: "30000",
            accident_medical: "30000",
            public_duty: "20000",
            luggage: "6000"
        };
        //执行爬虫
        httpRequest(data);

    }
    for (i = 0; i < days.length; i++) {
        data = {
            days: days[i],
            people_count: "1",
            accident_death: "500000",
            exigency_back: "50000",
            accident_medical: "50000",
            public_duty: "40000",
            luggage: "12000"
        };
        //执行爬虫
        httpRequest(data);

    }


}
//urlencode 函数
function urlencode(data) {
    try {
        var tempArr = [];
        for (var i in data) {
            var key = encodeURIComponent(i);
            var value = encodeURIComponent(data[i]);
            tempArr.push(key + '=' + value);
        }
        var urlParamsStr = tempArr.join('&');
        return urlParamsStr;
    } catch (err) {
        return '';
    }
}
//爬虫
function httpRequest(data) {
    if (data.days == "1d") {
        enddate = "2020/05/10";
    } else if (data.days == "7d") {
        enddate = "2020/05/16";
    } else {
        enddate = "2020/06/23";
    }
    if (data.accident_death == "100000") {
        items = "1517811714591";
    } else if (data.accident_death == "200000") {
        items = "1517811714592"
    } else {
        items = "1517811714593"
    }


    params = {
        "startdate": "2020/05/10",
        "enddate": enddate,
        "items": items,
        "peoplecount": "1",
        "planextendvalue": "",
        "productcode": "LAY_T"
    };

    request({
        url: url,
        method: "POST",
        json: false,
        headers: {
            "Accept": "application/json",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
            "Cookie": "epicc_tid=epicc.1578282117557422; _gscu_321070471=7828211745c42110; s_fid=51BB77C19E20D92E-16021A351FFE693E; vid=ef0bea9d6c25528ee3c6714b85e0baef; epicc_ntid=AAAAAV6CpgMv3h/HwX4WAg==; s_cc=true; xY6qiPJDsR=MDAwM2IyNTRmNDgwMDAwMDAwMjUwViIfAX4xNTg5MDA1OTE4; epiccnewEbsWeb=ZEH4Il-Lk-lINalaPGRMMr7xLYgGjvUsdMRmVLNwv7ywqpJ3t5jX!13307832; R4XlxlrGUk=MDAwM2IyNTRmNDgwMDAwMDAwNWUwbTV8KRcxNTg5MDA1OTE5; epiccnewEbsWebeproduct=8ur4HIPWoc28NZjOmeziayLEWBp6zToRjHJI1oBL0Lz2o1SKS-nT!891108929; _gscbrs_321070471=1; _gscs_321070471=89005934jn4lvv66|pv:1; _gscm=1; YXeuzFnfi6=MDAwM2IyNTRmNDgwMDAwMDAwNzYwXSZdTSQxNTg5MDA1OTIw; personalCenter=XDD4IBCBRmNI_SlDewDCxvome8AOFi8HUS0Wm3ZyIpP8hna_2DA_!-1076273103; udmp_cm_sign_321070471=1; s_getNewRepeat=1589005936068-Repeat; s_vnum=1609818118180%26vn%3D13; s_invisit=true; trkHmClickCoords=787%2C1552%2C3682%2Chttp%3A%2F%2Fwww.epicc.com.cn%2Fyiwaibaoxian%2Fhuaxuexian%2F%23%2Fmain",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36",

        },
        body: urlencode(params)

    }, function (err, res, body) {

        if (!err && res.statusCode == 200) {

            price = JSON.parse(body)[3]["originPremium"].toFixed(2).toString();
            result = {
                name: name,
                company: "中国人民保险",
                price: price
            };
            data = Object.assign(result, data);
            insertData(data)
        }

    })

}

